﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using TvControl;
using TvDatabase;
using TvLibrary.Log;
using TvEngine;
using TvLibrary.Interfaces;
using SetupTv;
using System.Windows.Forms;
using System.ServiceProcess;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using System.Threading;
namespace TvServerPlugin
{

    public partial class TV4HomeWebUIHostSetup : SetupTv.SectionSettings
    {

        ServiceController sc = null;
        BackgroundWorker worker = new BackgroundWorker();
        System.Timers.Timer timer = new System.Timers.Timer();


        public TV4HomeWebUIHostSetup()
            : this("")
        {
        }

        public TV4HomeWebUIHostSetup(String name)
            : base(name)
        {
            InitializeComponent();
        }

        public override void OnSectionDeActivated()
        {
            SavePlugInSettings();
            timer.Stop();
            base.OnSectionDeActivated();
        }

        public override void OnSectionActivated()
        {
            LoadPlugInSettings();
            CheckCoreServiceState();
            worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
            timer.AutoReset = true;
            timer.Interval = 1000;
            timer.Start();
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
            base.OnSectionActivated();
        }

        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            CheckCoreServiceState();
        }

        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            CheckCoreServiceState();
        }

        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            ChangeServiceState();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            SavePlugInSettings();
        }

        private void SavePlugInSettings()
        {
            Settings.httpPort = (int)nudPort.Value;
            Settings.webPwd = tbPassword.Text;
            Settings.webUid = tbUserName.Text;
            Settings.SaveSettings();

        }
        private void LoadPlugInSettings()
        {
            Settings.LoadSettings();
            tbPassword.Text = Settings.webPwd;
            tbUserName.Text = Settings.webUid;
            nudPort.Value = Settings.httpPort;
        }
        private void CheckCoreServiceState()
        {

            try
            {
                if (sc == null)
                {
                    sc = new System.ServiceProcess.ServiceController("TV4HomeCoreService");
                }
                sc.Refresh();
                lblCoreServiceStatus.Text = sc.Status.ToString();
                switch (sc.Status.ToString())
                {
                    case "Running":
                        btnCoreServiceControl.Text = "Stop Service";
                        break;
                    case "Stopped":
                        btnCoreServiceControl.Text = "Start Service";
                        break;
                }


            }
            catch (Exception ex)
            {
                Log.Error("TV4Home checking CoreService status failed: " + ex.StackTrace.ToString());
            }
        }

        private void btnAbout_Click(object sender, EventArgs e)
        {
            try
            {
                new AboutForm().ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace);
            }

        }

        private void btnCoreServiceControl_Click(object sender, EventArgs e)
        {
            worker.RunWorkerAsync();
        }

        private void ChangeServiceState()
        {

            try
            {
                ServiceController sc = new System.ServiceProcess.ServiceController("TV4HomeCoreService");
                switch (sc.Status)
                {
                    case ServiceControllerStatus.Stopped:
                        sc.Start();
                        break;
                    case ServiceControllerStatus.Running:
                        sc.Stop();
                        break;
                }

            }
            catch (Exception ex)
            {
                Log.Error("TV4Home Error changing service state" + ex.StackTrace.ToString());
            }
        }

        static string ProgramFilesx86()
        {
            if (8 == IntPtr.Size
                || (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
            {
                return Environment.GetEnvironmentVariable("ProgramFiles(x86)");
            }

            return Environment.GetEnvironmentVariable("ProgramFiles");
        }

    }
}
